#!/bin/bash

# 清屏并定义颜色变量
clear
RED='\e[1;31m'       # 红色（粗体）
GREEN='\e[1;32m'     # 绿色（粗体）
YELLOW='\e[1;33m'    # 黄色（粗体）
CYAN='\e[1;36m'      # 青色（粗体）
NC='\e[0m'           # 重置颜色

# 检查是否以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED}错误：请以 root 用户运行此脚本！${NC}"
    exit 1
fi

# 获取系统信息
OS=$(lsb_release -is)
VERSION_CODENAME=$(lsb_release -cs)

# 安装 sudo
echo -e "${GREEN}正在安装 sudo...${NC}"
apt-get install -y sudo

# 升级已安装的软件包
echo -e "${GREEN}正在升级已安装的软件包...${NC}"
sudo apt update && sudo apt upgrade -y

# 安装 Git
if ! command -v git &> /dev/null; then
    echo -e "${RED}Git 未安装，开始安装...${NC}"
    sudo apt update
    sudo apt install -y git curl
else
    echo -e "${GREEN}Git 已安装，无需重复安装。${NC}"
fi

# 安装 Node.js
if command -v node &> /dev/null; then
    echo -e "${GREEN}Node.js 已安装，无需重复安装。${NC}"
else
    echo -e "${RED}Node.js 未安装，开始安装...${NC}"
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash -
    sudo apt-get install -y nodejs
fi

# 安装中文字体
if dpkg -l | grep -q fonts-wqy-zenhei; then
    echo -e "${GREEN}中文字体已安装，无需重复安装。${NC}"
else
    echo -e "${RED}中文字体未安装，开始安装...${NC}"
    sudo apt-get install -y fonts-wqy-zenhei
fi

# 安装 FFmpeg
if command -v ffmpeg &> /dev/null; then
    echo -e "${GREEN}FFmpeg 已安装，无需重复安装。${NC}"
else
    echo -e "${RED}FFmpeg 未安装，开始安装...${NC}"
    sudo apt-get install -y ffmpeg
fi

# 安装 Chromium 浏览器
if [ "$OS" = "Ubuntu" ]; then
    if dpkg -l | grep -q chromium-browser; then
        echo -e "${GREEN}Chromium 浏览器已安装，无需重复安装。${NC}"
    else
        echo -e "${RED}Chromium 浏览器未安装，开始安装...${NC}"
        sudo apt-get install -y chromium-browser
    fi
elif [ "$OS" = "Debian" ]; then
    if dpkg -l | grep -q chromium; then
        echo -e "${GREEN}Chromium 浏览器已安装，无需重复安装。${NC}"
    else
        echo -e "${RED}Chromium 浏览器未安装，开始安装...${NC}"
        sudo apt-get install -y chromium
    fi
fi

# 安装 Redis
sudo apt-get install -y lsb-release curl gpg &> /dev/null
if dpkg -l | grep -q redis; then
    echo -e "${GREEN}Redis 已安装，无需重复安装。${NC}"
else
    echo -e "${RED}Redis 未安装，开始安装...${NC}"
    curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
    sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
    sudo apt-get update
    sudo apt-get install -y redis
    sudo systemctl enable redis-server
    echo -e "${GREEN}Redis 安装完成并已启用。${NC}"
fi

# 清屏并验证安装
clear
echo -e "${GREEN}正在验证安装...${NC}"

if command -v ffmpeg &> /dev/null; then
    echo -e "${GREEN}FFmpeg 版本:${NC} $(ffmpeg -version | head -n 1)"
else
    echo -e "${RED}FFmpeg 未安装！${NC}"
fi

if command -v node &> /dev/null; then
    echo -e "${GREEN}Node.js 版本:${NC} $(node --version)"
else
    echo -e "${RED}Node.js 未安装！${NC}"
fi

if command -v chromium &> /dev/null || command -v chromium-browser &> /dev/null; then
    echo -e "${GREEN}Chromium 浏览器已安装。${NC}"
else
    echo -e "${RED}Chromium 浏览器未安装！${NC}"
fi

if systemctl is-active --quiet redis-server; then
    echo -e "${GREEN}Redis 服务正在运行。${NC}"
    sudo systemctl status redis-server | grep Active
else
    echo -e "${RED}Redis 服务未运行！${NC}"
fi

# 提供用户选择安装插件
echo -e "${YELLOW}请选择要执行的命令:${NC}"
echo "1) Miao-Yunzai 多插件"
echo "2) TRSS-Yunzai 多插件"
echo "3) Miao-Yunzai 少插件"
echo "4) TRSS-Yunzai 少插件"
read -p "请输入你的选择: " CHOICE

case $CHOICE in
    1)
        echo "正在安装 Miao-Yunzai 多插件..."
        bash <(curl -L Gitee.com/Lovely-02/Install/raw/master/Yunzai/M)
        ;;
    2)
        echo "正在安装 TRSS-Yunzai 多插件..."
        bash <(curl -L Gitee.com/Lovely-02/Install/raw/master/Yunzai/T)
        ;;
    3)
        echo "正在安装 Miao-Yunzai 少插件..."
        bash <(curl -L Gitee.com/Lovely-02/Install/raw/master/Yunzai/ML)
        ;;
    4)
        echo "正在安装 TRSS-Yunzai 少插件..."
        bash <(curl -L Gitee.com/Lovely-02/Install/raw/master/Yunzai/TL)
        ;;
    *)
        echo "无效选择，请重新运行脚本。"
        exit 1
        ;;
esac